package com.xujie.property_warranty_system.controller;

import com.xujie.property_warranty_system.common.Result;
import com.xujie.property_warranty_system.dto.UserUpdateDTO;
import com.xujie.property_warranty_system.dto.PasswordChangeDTO;
import com.xujie.property_warranty_system.dto.CurrentUserVO;
import com.xujie.property_warranty_system.service.UserService;
import com.xujie.property_warranty_system.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/api/user")
@CrossOrigin(origins = "*")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private JwtUtil jwtUtil;
    
    /**
     * 获取当前用户信息
     */
    @GetMapping("/profile")
    public Result<CurrentUserVO> getCurrentUser(HttpServletRequest request) {
        try {
            String token = getTokenFromRequest(request);
            if (token == null) {
                return Result.error("未找到认证令牌");
            }
            
            String username = jwtUtil.getUsernameFromToken(token);
            CurrentUserVO userInfo = userService.getCurrentUserInfo(username);
            
            if (userInfo == null) {
                return Result.error("用户不存在");
            }
            
            return Result.success(userInfo);
        } catch (Exception e) {
            return Result.error("获取用户信息失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新用户信息
     */
    @PutMapping("/profile")
    public Result<String> updateProfile(@Valid @RequestBody UserUpdateDTO updateDTO, 
                                       HttpServletRequest request) {
        try {
            String token = getTokenFromRequest(request);
            if (token == null) {
                return Result.error("未找到认证令牌");
            }
            
            String username = jwtUtil.getUsernameFromToken(token);
            String message = userService.updateUserProfile(username, updateDTO);
            
            return Result.success(message);
        } catch (Exception e) {
            return Result.error("更新用户信息失败：" + e.getMessage());
        }
    }
    
    /**
     * 修改密码
     */
    @PutMapping("/password")
    public Result<String> changePassword(@Valid @RequestBody PasswordChangeDTO passwordDTO,
                                       HttpServletRequest request) {
        try {
            String token = getTokenFromRequest(request);
            if (token == null) {
                return Result.error("未找到认证令牌");
            }
            
            String username = jwtUtil.getUsernameFromToken(token);
            String message = userService.changePassword(username, passwordDTO);
            
            return Result.success(message);
        } catch (Exception e) {
            return Result.error("密码修改失败：" + e.getMessage());
        }
    }
    
    /**
     * 从请求中获取Token
     */
    private String getTokenFromRequest(HttpServletRequest request) {
        String bearerToken = request.getHeader("Authorization");
        if (bearerToken != null && bearerToken.startsWith("Bearer ")) {
            return bearerToken.substring(7);
        }
        return null;
    }
}
